package udp;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.io.IOException;


public class Send {
    private static final String DEFAULT_IP = "localhost";
    private static final int DEFAULT_PORT = 9999;

    public static void main(String[] args) {
        String targetIp = DEFAULT_IP;
        int targetPort = DEFAULT_PORT;

        // 未来可以考虑通过命令行参数或配置文件来设置 targetIp 和 targetPort

        try (DatagramSocket ds = new DatagramSocket()) {
            byte[] buf = "你好接收端".getBytes();
            DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName(targetIp), targetPort);
            ds.send(dp);
        } catch (IOException e) {
            // 捕获并处理更具体的异常类型，例如IOException
            e.printStackTrace();
            // 根据异常情况可能需要做一些清理工作或者重试逻辑
        } catch (Exception e) {
            // 捕获其他未预期的异常
            e.printStackTrace();
        }
    }
}
